package com.mallcai.bigdata.ladon.controller.system;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.cake.bigdata.starter.exception.error.CommonErrorCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mallcai.bigdata.ladon.client.service.IReportDataQueryService;
import com.mallcai.bigdata.ladon.controller.system.form.LoginForm;
import com.mallcai.bigdata.ladon.controller.system.form.RegForm;
import com.mallcai.bigdata.ladon.controller.system.form.UserForm;
import com.mallcai.bigdata.ladon.dpl.entity.system.User;
import com.mallcai.bigdata.ladon.service.system.IAccountService;
import com.mallcai.bigdata.ladon.vo.MyPageInfo;
import com.mallcai.bigdata.ladon.vo.UserVo;
import com.mallcai.bigdata.ladon.vo.system.MenuVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by oneape<oneape15@163.com>
 * Created 2019-05-16 11:55.
 * Modify:
 */
@RequestMapping("/sys/user")
@RestController
public class UserController {

    @Autowired
    private IAccountService         iAccountService;

    @RequestMapping("/add")
    public String addUser(@RequestBody @Validated(value = UserForm.AddCheck.class) UserForm form) {
        User user = form.toEntity();

        int status = iAccountService.addUser(user, form.getRoleIds());
        if (status <= 0) {
            throw new BusinessException("添加用户失败");
        }
        return "添加用户成功";
    }

    @RequestMapping("/edit")
    public String editUser(@RequestBody @Validated(value = UserForm.EditCheck.class) UserForm form) {
        User user = form.toEntity();

        int status = iAccountService.updateUser(user, form.getRoleIds());
        if (status <= 0) {
            throw new BusinessException("修改用户失败");
        }

        return "修改用户成功";
    }

    @RequestMapping("/del")
    public String delUser(@RequestBody @Validated(value = UserForm.IdCheck.class) UserForm form) {
        User user = form.toEntity();
        int status = iAccountService.deleteUser(user);
        if (status <= 0) {
            throw new BusinessException("删除用户失败");
        }

        return "删除用户成功";
    }


    @RequestMapping("/resetPwd")
    public String resetPwd(@RequestBody @Validated(value = UserForm.IdCheck.class) UserForm form) {
        int status = iAccountService.resetPwd(form.getUserId());
        if (status <= 0) {
            throw new BusinessException("重置密码失败");
        }

        return "重置密码成功";
    }

    @RequestMapping("/changePwd")
    public String changePwd(@RequestBody @Validated(value = UserForm.ChangePwdCheck.class) UserForm form) {
        int status = iAccountService.changePwd(form.getOldPwd(), form.getNewPwd());
        if (status <= 0) {
            throw new BusinessException("修改密码失败");
        }
        return "修改密码成功";
    }

    @RequestMapping("/getByPage")
    public MyPageInfo getByPage(@RequestBody @Validated UserForm form) {
        PageHelper.startPage(form.getCurrentPage(), form.getPageSize());
        List<User> models = iAccountService.getByParams(form.getParams());
        PageInfo<User> pageInfo = new PageInfo<>(models);

        return new MyPageInfo<>(pageInfo);
    }

    @RequestMapping("/users")
    public List getUsers() {
        List<User> users = iAccountService.getAllByAppId();

        List<UserVo> vos = new ArrayList<>();
        if (users != null && users.size() > 0) {
            for (User user : users) {
                UserVo vo = new UserVo();
                BeanUtils.copyProperties(user, vo);
                vos.add(vo);
            }
        }
        return vos;
    }

    @RequestMapping("/getCurrent")
    public UserVo getCurrent() {
        User user = iAccountService.getCurrentUser();
        if (user == null) {
            throw new BusinessException(CommonErrorCode.UNAUTHORIZED);
        }
        UserVo vo = new UserVo();
        BeanUtils.copyProperties(user, vo);
        return vo;
    }

    @RequestMapping("/login")
    public Object doLogin(@RequestBody @Validated(value = LoginForm.LoginCheck.class) LoginForm loginForm) {
        UserVo vo = iAccountService.login(loginForm.getUserName(), loginForm.getPassword());
        if (vo == null) {
            throw new BusinessException(CommonErrorCode.UNAUTHORIZED.getCode(), "登录失败");
        }

        // 获取资源权限
        Map<String, Object> map;
        try {
            map = com.mallcai.bigdata.ladon.tools.BeanUtils.objectToMap(vo);
            Map<String, List<Integer>> optPermissions = iAccountService.getResOptPermission(vo.getId());

            map.put("optPermissions", optPermissions);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    /**
     * 获取当前用户的菜单列表
     *
     * @return List
     */
    @RequestMapping("/menuTree")
    public List<MenuVo> getUserMenuTree() {
        List<MenuVo> menus = iAccountService.getCurrentMenus();
        return menus;
    }

    /**
     * 用户注册
     *
     * @param regForm RegForm
     * @return ApiResult
     */
    @RequestMapping("/reg")
    public String regUser(@RequestBody @Validated(value = RegForm.RegCheck.class) RegForm regForm) {
        User user = regForm.toEntity();

        int status = iAccountService.regUser(user);
        if (status <= 0) {
            throw new BusinessException("注册用户失败");
        }

        return "注册成功, 请到邮箱中激活你的账号~";
    }

}
